<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function _new(Fn,...args) {
        var obj = {} // 创建空对象
        obj.__proto__ = Fn.prototype // 将obj的原型链__proto__指向构造函数的原型prototype
        obj.__proto__.constructor = Fn // 在原型链 __proto__上设置构造函数的构造器constructor，为了实例化Fn
        Fn.apply(obj, args) // 执行Fn，并将构造函数Fn执行obj
        return obj // 返回结果
      }

      function Fn(name) {
        this.name = name
        this.sayName = function () {
          console.log(this.name)
        }
      }

      Fn.prototype.say = function (msg) {
        console.log(`${this.name} ${msg}`)
      }

      const fn = _new(Fn, 'Ksmith')
      console.log('🚀🚀🚀wimi======>>>fn', fn)
      fn.say('Hello World') // Ksmith Hello World
      fn.sayName() // Ksmith
      console.log(fn.name) // Ksmith
    </script>
  </body>
</html>
